package com.xh.bussiness.xkw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.xkw.bean.dto.XkwTiKuKnowledgeDto;
import com.xh.bussiness.xkw.dao.XkwTikuKnowledgeMapper;
import com.xh.bussiness.xkw.model.XkwTikuKnowledge;
import com.xh.bussiness.xkw.service.IXkwTiKuKnowledgeService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 知识点信息表 服务实现类
 * </p>
 *
 * @author Jqm
 * @since 2021-05-20
 */
@Service
public class XkwTiKuKnowledgeServiceImpl extends BaseServiceImpl<XkwTikuKnowledgeMapper, XkwTikuKnowledge> implements IXkwTiKuKnowledgeService {

    @Override
    public List<XkwTiKuKnowledgeDto> queryKnowledgeBySubjectCode(String subjectCode) {
        QueryWrapper<XkwTikuKnowledge> qw = new QueryWrapper<>();
        qw.eq("subject",subjectCode);
        List<XkwTiKuKnowledgeDto> knowledgeList =  this.selectList(qw,XkwTiKuKnowledgeDto.class);

        List<XkwTiKuKnowledgeDto> knowledgeDtos = new ArrayList<>();
        for (XkwTiKuKnowledgeDto knowledgeDto : knowledgeList){
           if("0".equals(knowledgeDto.getParentId())) {
               knowledgeDtos.add(findChildes(knowledgeDto,knowledgeList));
           }
        }
        return knowledgeDtos;
    }

    private XkwTiKuKnowledgeDto findChildes(XkwTiKuKnowledgeDto knowledge,List<XkwTiKuKnowledgeDto> knowledgeList){
        for (XkwTiKuKnowledgeDto knowledgeDto : knowledgeList){
            if(knowledge.getId().equals(knowledgeDto.getParentId())){
                if(knowledgeDto.getChilds()==null){
                    knowledgeDto.setChilds(new ArrayList<XkwTiKuKnowledgeDto>());
                }
                knowledge.getChilds().add(findChildes(knowledgeDto,knowledgeList));
            }
        }
        return knowledge;
    }
}
